package com.bizdk.generator.core;

import com.bizdk.generator.core.DbType;
import com.bizdk.generator.core.GenDataSrc;
import oracle.jdbc.OracleConnection;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

/**
 * DB工具类
 */
public class DbUtils {
    
    private static final int CONNECTION_TIMEOUTS_SECONDS = 6;
    
    /**
     * 获得数据库连接
     */
    public static Connection getConnection(GenDataSrc genDataSrc) throws ClassNotFoundException, SQLException {
        DriverManager.setLoginTimeout(CONNECTION_TIMEOUTS_SECONDS);
        Class.forName(genDataSrc.getDbType().getDriverClass());
        
        Connection connection = DriverManager.getConnection(genDataSrc.getConnUrl(), genDataSrc.getUsername(),
                genDataSrc.getPassword());
        if (genDataSrc.getDbType() == DbType.Oracle) {
            ((OracleConnection) connection).setRemarksReporting(true);
        }
        
        return connection;
    }
    
    
}